package com.xiaofengstu.mcams.web.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaofengstu.mcams.dto.UserDTO;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Mapper
public interface TUserMapper extends BaseMapper<TUser> {

  @Select("select user_id,username,campus_name campus ,dept_name dept,role_name role,email,mobile,sex \n" +
      "from t_user tu join t_role tr on tu.role_id = tr.role_id \n" +
      "join t_dept td on tu.dept_id =td.dept_id \n" +
      "join t_campus tc on tc.campus_id = td.campus_id \n")
  Page<UserDTO> getUserList(Page page);

  @Select("select count(*) \n" +
      "from t_user tu join t_role tr on tu.role_id = tr.role_id \n" +
      "join t_dept td on tu.dept_id =td.dept_id \n" +
      "join t_campus tc on tc.campus_id = td.campus_id")
  Integer getUserCount();

  @Select("<script> select user_id,username,campus_name campus ,dept_name dept,role_name role,email,mobile,sex \n" +
      "from t_user tu join t_role tr on tu.role_id = tr.role_id \n" +
      "join t_dept td on tu.dept_id =td.dept_id \n" +
      "join t_campus tc on tc.campus_id = td.campus_id " +
      "where 1=1 <if test='username!=null'>and tu.username like CONCAT('%',#{username},'%') </if>" +
      "<if test='campusId !=null'>and tc.campus_id = #{campusId} </if>" +
      "<if test='deptId!=null'>and td.dept_id = #{deptId} </if>" +
      "</script>")
  List<UserDTO> searchBy(@Param("username") String username, @Param("campusId") Integer campusId, @Param("deptId") Integer deptId);
}
